package com.heima.skydemo.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.skydemo.domin.common.PageResult;
import com.heima.skydemo.domin.dto.SetmealQuerry;
import com.heima.skydemo.domin.vo.SetmealVo;
import com.heima.skydemo.entity.Category;
import com.heima.skydemo.entity.Setmeal;
import com.heima.skydemo.mapper.SetmealMapper;
import com.heima.skydemo.service.ISetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-04-15
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {
@Autowired
private CategoryServiceImpl categoryService;
    @Override
    public PageResult pageQuerry(SetmealQuerry setmealQuerry) {
        PageHelper.startPage(setmealQuerry.getPage(), setmealQuerry.getPageSize());
        Page<Setmeal> pageQuery = (Page<Setmeal>) list();
        Page<SetmealVo> setmealVos = new Page<>();
        for (Setmeal setmeal : pageQuery){
            SetmealVo setmealVo = new SetmealVo();
            BeanUtils.copyProperties(setmeal,setmealVo);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealVo.setCategoryName(category.getName());
            setmealVos.add(setmealVo);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal(setmealVos.getTotal());
        pageResult.setRecords(setmealVos.getResult());
        return pageResult;
    }
}
